

package cn.shengchao.examstar.distributor.gateway.controller;

import cn.shengchao.dubbo.bi.PlatformUserArchBIService;
import cn.shengchao.examstar.distributor.gateway.controller.query.MemberPageQuery;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.base.TransResult;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeNodeInfo;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeNodeInfoService;
import cn.shengchao.examstar.rpc.distribute.dto.DistributePersonUserCreateDto;
import cn.shengchao.examstar.distributor.gateway.service.DistributorService;
import cn.shengchao.examstar.distributor.gateway.service.message.EmployeeListItem;
import cn.shengchao.examstar.distributor.gateway.service.message.ExtensionOfficerListItem;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeSaleRoleEnum;
import cn.shengchao.util.helper.LoginHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.core.validation.CheckUtils;

import java.util.List;

@RestController
@Slf4j
@CrossOrigin("${project.cross-origin}")
@RequestMapping("/api/${project.version}/distributor/team")
public class DistributorTeamController {

    @Autowired
    private DistributorService distributorService;

    @Autowired
    private PlatformUserArchBIService platformUserArchBIService;

    @Autowired
    private DistributeNodeInfoService distributeNodeInfoService;

    /**
     * 员工列表查询
     */
    @GetMapping("employee")
    public List<EmployeeListItem> getEmployeeList(@RequestParam(required = false, value = "searchName") String searchName) {
        return distributorService.getEmployeeList(LoginHelper.getUserId(), searchName);
    }
    /**
     * 查询推广员列表
     */
    @GetMapping("extensionOfficer/page")
    public PageResult<ExtensionOfficerListItem> getExtensionOfficerPage(MemberPageQuery memberPageQuery) {
        memberPageQuery.setDistributeUserId(LoginHelper.getUserId());
        return distributorService.getExtensionOfficerPage(memberPageQuery);
    }
    /**
     * 查询推广员列表
     */
    @GetMapping("extensionOfficer")
    public List<ExtensionOfficerListItem> getExtensionOfficerList(@RequestParam(required = false, value = "searchName") String searchName) {
        return distributorService.getExtensionOfficerList(LoginHelper.getUserId(), searchName);
    }

    /**
     * 接/冻结员工
     */
    @PutMapping("employee/{id}/status:{status}")
    public void updateEmployeeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        distributorService.updateEmployeeStatus(id, status);
    }

    /**
     * 成为员工
     */
    @PostMapping("employee")
    public void becomeEmployee(@RequestBody DistributePersonUserCreateDto distributePersonUserCreateDto) {
        DistributeNodeInfo distributeNodeInfo = distributeNodeInfoService.getDistributeNodeInfoInCode(LoginHelper.getUserId(),
                List.of(DistributeSaleRoleEnum.ROLE_MKMG, DistributeSaleRoleEnum.ROLE_DSPD, DistributeSaleRoleEnum.ROLE_SLMB,DistributeSaleRoleEnum.ROLE_DSPD_EMP));
        if(distributeNodeInfo!=null){
            CheckUtils.throwIf(distributeNodeInfo.getDistributeRoleCode().equals(DistributeSaleRoleEnum.ROLE_DSPD_EMP.getCode()), "您已经是员工，无需重复操作");
            CheckUtils.throwIf(distributeNodeInfo.getDistributeRoleCode().equals(DistributeSaleRoleEnum.ROLE_DSPD.getCode()), "您已经是加盟商,无法成为员工!");
            CheckUtils.throwIf(distributeNodeInfo.getDistributeRoleCode().equals(DistributeSaleRoleEnum.ROLE_SLMB.getCode()), "您是会员用户,无法成为员工!");
            CheckUtils.throwIf(distributeNodeInfo.getDistributeRoleCode().equals(DistributeSaleRoleEnum.ROLE_MKMG.getCode()), "您是市场经理,无法成为员工!");
        }
        distributePersonUserCreateDto.setUserId(LoginHelper.getUserId());
        distributePersonUserCreateDto.setDistributeRoleCode(DistributeSaleRoleEnum.ROLE_DSPD_EMP.getCode());
        TransResult<String> result = platformUserArchBIService.createSaleDistributeJmsygAndRoleChain(distributePersonUserCreateDto);
        CheckUtils.throwIf(!result.getSuccess(), result.getMessage());
    }

}
